//! @Author: DengLibin 榆霖
//! @Date:  2022-07-11 15:06:05
//! @Description: 

use std::collections::HashMap;

use async_dao::{mysql_conn_pool, entity::user::User, user_dao};
use tokio::runtime::{Runtime};
#[test]
pub fn test_insert(){

    let pool = mysql_conn_pool::get_conn_pool();
    let u = User{
        id: 1,
        deleted: false,
        create_time: None,
        update_time: None,
        create_by: -1,
        update_by: -1,
        user_login_name: String::from("lisi"),
        name: String::from("李四"),
        phone_num: String::from("18248987855"), 
        passwords: String::from("123456"),
        image_url: None,
    };
    let f = user_dao::insert(&u, &pool);
    let runtime = Runtime::new().unwrap();
    
    let r = runtime.block_on(f);
    match r {
        Ok(()) => println!("--------------------------->成功"),
        Err(e) => println!("{}", e)
    }
}


/// @Author: DengLibin 榆霖
/// @Date:  2022-07-01 13:48:01
/// @Description: 批量添加
#[test]
pub fn test_insert_batch(){

    let pool = mysql_conn_pool::get_conn_pool();
    let users = vec![User{
        id: 1,
        deleted: false,
        create_time: None,
        update_time: None,
        create_by: -1,
        update_by: -1,
        user_login_name: String::from("lisi-1"),
        name: String::from("李四-1"),
        phone_num: String::from("18248987856"),
        passwords: String::from("123456"),
        image_url: None,
    },
    User{
        id: 2,
        deleted: false,
        create_time: None,
        update_time: None,
        create_by: -1,
        update_by: -1,
        user_login_name: String::from("lisi-2"),
        name: String::from("李四-2"),
        phone_num: String::from("18248987857"),
        passwords: String::from("123456"),
        image_url: None,
    },
    User{
        id: 3,
        deleted: false,
        create_time: None,
        update_time: None,
        create_by: -1,
        update_by: -1,
        user_login_name: String::from("lisi-3"),
        name: String::from("李四-3"),
        phone_num: String::from("18248987858"),
        passwords: String::from("123456"),
        image_url: None,
    },
    User{
        id: 4,
        deleted: false,
        create_time: None,
        update_time: None,
        create_by: -1,
        update_by: -1,
        user_login_name: String::from("lisi-4"),
        name: String::from("李四-4"),
        phone_num: String::from("18248987859"),
        passwords: String::from("123456"),
        image_url: None,
    }];
    let runtime = Runtime::new().unwrap();
    let r = runtime.block_on(user_dao::insert_batch(&users, &pool));
    match r{
        Ok(()) => {println!("添加结果成功------------------")},
        Err(e) => {println!("{}", e)}
    }
}

/// @Author: DengLibin 榆霖
/// @Date:  2022-07-01 14:00:29
/// @Description: 查询所有
#[test]
pub fn test_get_all(){

    let pool = mysql_conn_pool::get_conn_pool();
    let runtime = Runtime::new().unwrap();

    let f = user_dao::get_all(&pool);
    let users = runtime.block_on(f).unwrap();
    for u in &users {
        println!("{}", u);
    }
}

/// @Author: DengLibin 榆霖
/// @Date:  2022-07-05 09:59:06
/// @Description: 分页查询
#[test]
pub fn test_get_page() {
    env_logger::builder()
        .filter_level(log::LevelFilter::Trace)//级别
        .init();
   
    let runtime = Runtime::new().unwrap();
    let pool = mysql_conn_pool::get_conn_pool();
    let page_info = runtime.block_on(user_dao::get_page(1, 10, &pool)).unwrap();
    
    println!("page: {}, size: {}, total: {}", page_info.page, page_info.size, page_info.total);
    for u in &page_info.list {
        println!("{}", u);
    }
}

/// @Author: DengLibin 榆霖
/// @Date:  2022-07-05 10:11:49
/// @Description: 删除
#[test]
pub fn test_delete() {
    let pool = mysql_conn_pool::get_conn_pool();
    let runtime = Runtime::new().unwrap();
    let r = runtime.block_on(user_dao::delete(1, &pool));
    match r{
        Ok(b) => {println!("删除结果:{}", b)},
        Err(e) => {println!("{}", e)}
    }
}
/// @Author: DengLibin 榆霖
/// @Date:  2022-07-05 10:59:02
/// @Description: 更新
#[test]
pub fn test_update() {
    let pool = mysql_conn_pool::get_conn_pool();
    let mut map: HashMap<&str, &str>= HashMap::new();
    map.insert("name", "王五");
    map.insert("user_login_name", "wangwu");
    let runtime = Runtime::new().unwrap();
    let r = runtime.block_on(user_dao::update(map, 2, &pool));
    match r{
        Ok(b) => {println!("更新结果:{}", b)},
        Err(e) => {println!("{}", e)}
    }
}